package org.project.d.network.start;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.CombinedChannelDuplexHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import org.project.d.network.Connection;
import org.project.d.network.coder.TcpCombinedChannelDuplexHandler;
import org.project.d.network.coder.UDPPacketDecoder;
import org.project.d.network.coder.UDPPacketEncoder;
import org.project.d.network.coder.UdpCombinedChannelDuplexHandler;
import org.project.d.network.handler.ConnectionEvent;
import org.project.d.network.handler.ConnectionHandler;
import org.project.d.network.handler.HeartEvent;
import org.project.d.network.handler.MsgDispatcherHandler;

/**
 * @author lin
 * @version V1.0
 * @description
 * @date 2020/9/22 2:13 下午
 */
public class UdpServer extends NettyUDPServer {
    protected ChannelHandlerInit channelHandlerInit = new ChannelHandlerInit() {
//        private final ChannelHandler idleStateHandler=new IdleStateHandler(0, 0, 60);
        private final ChannelHandler connectionHand=new ConnectionHandler(ConnectionEvent.builder().heartEvent(new HeartEvent() {
            @Override
            public void readerIdle(IdleStateEvent idleStateEvent, Connection connection) {
                connection.close();
            }

            @Override
            public void writerIdle(IdleStateEvent idleStateEvent, Connection connection) {

            }

            @Override
            public void allIdle(IdleStateEvent idleStateEvent, Connection connection) {
                connection.close();
            }
        }).build());
        private final ChannelHandler msgDispatcherHandler=new MsgDispatcherHandler();

        @Override
        public void channelInit(ChannelPipeline pipeline) {
            pipeline.addLast(new UdpCombinedChannelDuplexHandler());
            pipeline.addLast(connectionHand);
            pipeline.addLast(msgDispatcherHandler);

        }
    };

    private ChannelOptionInit channelOptionInit;
    public UdpServer(int port) {
        super(port);
    }

    public UdpServer(int port, String host, ChannelHandlerInit channelHandlerInit) {
        super(port, host);
        if (channelHandlerInit!=null){
            this.channelHandlerInit = channelHandlerInit;
        }
    }

    public UdpServer(int port, String host, ChannelHandlerInit channelHandlerInit, ChannelOptionInit channelOptionInit) {
        super(port, host);
        if (channelHandlerInit!=null){
            this.channelHandlerInit = channelHandlerInit;
        }
        if (channelOptionInit!=null){
            this.channelOptionInit = channelOptionInit;
        }
    }

    public UdpServer(int port, String host) {
        super(port, host);
    }

    public UdpServer(int port, ChannelHandlerInit channelHandlerInit) {
        super(port);
        this.channelHandlerInit = channelHandlerInit;
    }

    public UdpServer(int port, ChannelHandlerInit channelHandlerInit, ChannelOptionInit channelOptionInit) {
        super(port);
        this.channelHandlerInit = channelHandlerInit;
        this.channelOptionInit = channelOptionInit;
    }

    @Override
    protected void initPipeline(ChannelPipeline pipeline) {
        channelHandlerInit.channelInit(pipeline);
    }

    @Override
    protected void initOptions(Bootstrap b) {
        if (channelOptionInit==null){
            super.initOptions(b);
        }else {
            channelOptionInit.channelInit(b);
        }
    }
}
